package org.groupg.practice;

import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.select.FromItem;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectItem;

public class SelectTraverser {
    public static void traverseSelect(Select select) {
        // 获取SELECT主体
        PlainSelect plainSelect = select.getPlainSelect();

        // 1. 获取字段列表
        System.out.println("\n字段列表:");
        for (SelectItem item : plainSelect.getSelectItems()) {
            Expression expr = (item).getExpression();
            System.out.println(" - " + expr);

        }

        // 2. 获取数据源
        FromItem fromItem = plainSelect.getFromItem();
        if (fromItem instanceof Table table) {
            System.out.println("\n数据表: " + table.getName());
        }

        // 3. 获取WHERE条件
        Expression where = plainSelect.getWhere();
        if (where != null) {
            System.out.println("\nWHERE条件: " + where);
        }
    }
}